<?php
class Util {

    public static function removeAllFilesFromDir($dir) {
    	$files = glob($dir."*");
        foreach($files as $file){ // iterate files
		  if(is_file($file))
		    unlink($file); // delete file
		}
    }

    public static function deleteDir($src) {
		$dir = opendir($src);
	    while(false !== ( $file = readdir($dir)) ) { 
	        if (( $file != '.' ) && ( $file != '..' )) { 
	            if ( is_dir($src . '/' . $file) ) { 
	                Util::deleteDir($src . '/' . $file); 
	            } 
	            else { 
	                unlink($src . '/' . $file); 
	            } 
	        } 
	    } 
	    rmdir($src);
	    closedir($dir);
	}

	public static function formatMoney($str, $amount)
	{
		return sprintf($str, number_format($amount));
	}

	public static function checkIfSolvedRequest($requestStatus)
	{
		return $requestStatus == Config::get('globals.SOLVED_REQUEST_STATUS');
	}

	public static function checkIfDispatchedItemRequest($itemRequestStatus)
	{
		return $itemRequestStatus == Config::get('globals.DISPATCHED_ITEM_REQUEST_STATUS');
	}

	public static function checkIfRejectedItemRequest($itemRequestStatus)
	{
		return $itemRequestStatus == Config::get('globals.REJECTED_ITEM_REQUEST_STATUS');
	}

	public static function userHasPendingRequests($requests)
	{
		$response = false;
		if(!$requests->isEmpty())
		{
			foreach($requests as $request)
			{
				if(!Util::checkIfSolvedRequest($request->request_status_fk))
				{
					return true;
				}	
			}

		}
		return $response;
	}

	public static function generateRandomString($length = 10) {
	    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
	    $charactersLength = strlen($characters);
	    $randomString = '';
	    for ($i = 0; $i < $length; $i++) {
	        $randomString .= $characters[rand(0, $charactersLength - 1)];
	    }
	    return $randomString;
	}
}
?>